﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
 

public class Form1 : Form
{
    // Fields
    private string backpath = (Application.StartupPath + @"\backup\");
    private Button button1;
    private IContainer components = null;
    private int count = 0;
    private string errorinfo = string.Empty;
    private Label lab_file;
    private Label lab_process;
    private string originalpath = (Application.StartupPath + @"\");
    private ProgressBar progressBar1;
    private ProgressBar progressBar2;
    private string strUrl = "http://agent.yisocms.com/UpDateNew/";
    private Thread thread = null;
    private TextBox txt_Contect;

    // Methods
    public Form1()
    {
        this.InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (this.components != null))
        {
            this.components.Dispose();
        }
        base.Dispose(disposing);
    }

    public bool DownloadFile(string URL, string filename, ProgressBar prog, Label label1)
    {
        float num = 0f;
        Stream responseStream = null;
        Stream stream2 = null;
        try
        {
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(URL);
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            long contentLength = response.ContentLength;
            if (prog != null)
            {
                prog.Maximum = (int) contentLength;
            }
            responseStream = response.GetResponseStream();
            stream2 = new FileStream(filename, FileMode.Create);
            long num3 = 0;
            byte[] buffer = new byte[0x400];
            int count = responseStream.Read(buffer, 0, buffer.Length);
            while (count > 0)
            {
                num3 = count + num3;
                Application.DoEvents();
                stream2.Write(buffer, 0, count);
                if (prog != null)
                {
                    prog.Value = (int) num3;
                }
                count = responseStream.Read(buffer, 0, buffer.Length);
                num = (((float) num3) / ((float) contentLength)) * 100f;
                label1.Text = "当前文件下载进度" + Convert.ToInt32(num).ToString() + "%";
                Application.DoEvents();
            }
            stream2.Close();
            responseStream.Close();
            return true;
        }
        catch (Exception)
        {
            MessageBox.Show(this, "由于网络问题更新失败，请稍后重试！", "更新失败", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            stream2.Close();
            responseStream.Close();
            return false;
        }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Control.CheckForIllegalCrossThreadCalls = false;
        this.thread = new Thread(new ThreadStart(this.loaddown));
        this.thread.Start();
    }

    private void InitializeComponent()
    {
        ComponentResourceManager manager = new ComponentResourceManager(typeof(Form1));
        this.progressBar2 = new ProgressBar();
        this.button1 = new Button();
        this.lab_file = new Label();
        this.txt_Contect = new TextBox();
        this.lab_process = new Label();
        this.progressBar1 = new ProgressBar();
        base.SuspendLayout();
        this.progressBar2.Location = new Point(0x18, 0x106);
        this.progressBar2.Name = "progressBar2";
        this.progressBar2.Size = new Size(0x132, 0x12);
        this.progressBar2.TabIndex = 1;
        this.button1.Enabled = false;
        this.button1.Location = new Point(0xff, 0x128);
        this.button1.Name = "button1";
        this.button1.Size = new Size(0x4b, 0x17);
        this.button1.TabIndex = 2;
        this.button1.Text = "取消";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new EventHandler(this.button1_Click);
        this.lab_file.AutoSize = true;
        this.lab_file.BackColor = Color.Transparent;
        this.lab_file.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
        this.lab_file.Location = new Point(0x1b, 0xf7);
        this.lab_file.Name = "lab_file";
        this.lab_file.Size = new Size(0x41, 12);
        this.lab_file.TabIndex = 0;
        this.lab_file.Text = "下载文件：";
        this.txt_Contect.Font = new Font("宋体", 10f);
        this.txt_Contect.Location = new Point(0x18, 0x34);
        this.txt_Contect.Multiline = true;
        this.txt_Contect.Name = "txt_Contect";
        this.txt_Contect.ScrollBars = ScrollBars.Vertical;
        this.txt_Contect.Size = new Size(0x132, 0x8f);
        this.txt_Contect.TabIndex = 3;
        this.lab_process.AutoSize = true;
        this.lab_process.BackColor = Color.Transparent;
        this.lab_process.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
        this.lab_process.Location = new Point(0x1b, 0xcd);
        this.lab_process.Name = "lab_process";
        this.lab_process.Size = new Size(0x41, 12);
        this.lab_process.TabIndex = 0;
        this.lab_process.Text = "下载文件：";
        this.progressBar1.Location = new Point(0x18, 0xdd);
        this.progressBar1.Name = "progressBar1";
        this.progressBar1.Size = new Size(0x132, 0x11);
        this.progressBar1.TabIndex = 1;
        base.AutoScaleDimensions = new SizeF(6f, 12f);
      
 
        base.ClientSize = new Size(0x159, 340);
        base.ControlBox = false;
        base.Controls.Add(this.txt_Contect);
        base.Controls.Add(this.button1);
        base.Controls.Add(this.progressBar1);
        base.Controls.Add(this.progressBar2);
        base.Controls.Add(this.lab_process);
        base.Controls.Add(this.lab_file);
        this.Font = new Font("宋体", 9f);
     
 
        base.MaximizeBox = false;
        base.Name = "Form1";
        base.StartPosition = FormStartPosition.CenterScreen;
        this.Text = "易搜应用自动在线升级";
        base.FormClosing += new FormClosingEventHandler(this.Form1_FormClosing);
        base.Load += new EventHandler(this.Form1_Load);
        base.ResumeLayout(false);
        base.PerformLayout();
    }

    private void loaddown()
    {
        int num2;
        Exception exception;
        XmlDocument document = new XmlDocument();
        document.Load(Application.StartupPath + @"\AutoUpdaterNew.xml");
        XmlElement documentElement = document.DocumentElement;
        XmlNode node = documentElement.SelectSingleNode("description");
        StringBuilder builder = new StringBuilder();
        builder.Append("更新内容如下：\r\n");
        int num = int.Parse(node.Attributes["count"].Value);
        for (num2 = 0; num2 < num; num2++)
        {
            XmlNode node2 = node.ChildNodes[num2];
            builder.Append(node2.Attributes["contect"].Value + "\r\n");
        }
        this.txt_Contect.Text = builder.ToString();
        Process[] processes = Process.GetProcesses();
        this.lab_file.Text = "关闭程序进程";
        foreach (Process process in processes)
        {
            if (process.ProcessName == "RankSystem")
            {
                process.Kill();
            }
        }
        Thread.Sleep(0x5dc);
        this.progressBar2.Value = 0;
        this.progressBar2.Minimum = 0;
        this.progressBar2.Step = 1;
        XmlNode node3 = documentElement.SelectSingleNode("filelist");
        this.count = int.Parse(node3.Attributes["count"].Value);
        this.progressBar2.Maximum = this.count + 1;
        this.lab_file.Text = "正在更新......";
        ArrayList list = new ArrayList();
        for (num2 = 0; num2 < this.count; num2++)
        {
            XmlNode node4 = node3.ChildNodes[num2];
            string str = node4.Attributes["name"].Value;
            list.Add(str);
            try
            {
                if (File.Exists(this.backpath + str))
                {
                    File.Delete(this.backpath + str);
                }
                if (File.Exists(this.originalpath + str))
                {
                    File.Copy(this.originalpath + str, this.backpath + str);
                    File.Delete(this.originalpath + str);
                }
            }
            catch (Exception exception1)
            {
                exception = exception1;
                MessageBox.Show(this, exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.progressBar2.Value++;
            if (!this.DownloadFile(this.strUrl + str, this.originalpath + str, this.progressBar1, this.lab_process))
            {
                foreach (string str2 in list)
                {
                    if (File.Exists(this.backpath + str2))
                    {
                        if (File.Exists(this.originalpath + str2))
                        {
                            File.Delete(this.originalpath + str2);
                        }
                        File.Copy(this.backpath + str2, this.originalpath + str2);
                    }
                    if (File.Exists(this.backpath + str2))
                    {
                        File.Delete(this.backpath + str2);
                    }
                }
                if (File.Exists(this.backpath + "AutoUpdaterNew.xml"))
                {
                    File.Delete(this.backpath + "AutoUpdaterNew.xml");
                }
                base.Close();
                return;
            }
        }
        foreach (string str2 in list)
        {
            if (File.Exists(this.backpath + str2))
            {
                File.Delete(this.backpath + str2);
            }
        }
        if (File.Exists(this.backpath + "AutoUpdaterNew.xml"))
        {
            File.Delete(this.backpath + "AutoUpdaterNew.xml");
        }
        try
        {
            this.progressBar2.Value = this.progressBar2.Maximum;
            this.progressBar1.Value = this.progressBar1.Maximum;
            Thread.Sleep(0x3e8);
            this.lab_file.Text = "更新完成...";
            MessageBox.Show(this, "更新成功,请手动重启本软件！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            base.Close();
        }
        catch (Exception exception2)
        {
            exception = exception2;
            MessageBox.Show(this, exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
    }
}

 
